const { cleanDatEnglishText } = require("../src/utils/datTextCleaner");

// 实际的DAT条目
const realDatEntry =
  "#12.879#19.571##################*^We$we$$$$$^ ^shall$shall$$$$$^ ^lodge$lodge$$$$$^ ^a$a$$$$$^ ^claim$claim$$$$$^ ^for all$for all$$$$$^ ^the$the$$$$$^ ^losses$loss$$$$$^ ^incurred$incur$$$$$^ ^as$as$$$$$^ ^a$a$$$$$^ ^consequence$consequence$◆$$$$^ ^of$of$$$$$^ ^your$your$$$$$^ ^failure$failure$$$$$^ ^to$to$$$$$^ ^ship$ship$$$$$^ ^our$our$$$$$^ ^order$order$$$$$^ ^in time$in time$$$$$^.^#由于你方未能及时交货，我方将向你方提出由此而遭受的全部损失的索赔。";

console.log("测试实际DAT条目:\n");
console.log(`原始条目: ${realDatEntry}\n`);

// 提取英语部分（在第一个#和最后一个#之间）
const parts = realDatEntry.split("#");
const englishPart = parts[4]; // 英语部分在第5个#之后
const chinesePart = parts[5]; // 中文部分在第6个#之后

console.log(`提取的英语部分: ${englishPart}\n`);
console.log(`提取的中文部分: ${chinesePart}\n`);

// 使用我们的工具清理英语部分
const cleanedEnglish = cleanDatEnglishText(englishPart);
console.log(`清理后的英语部分: ${cleanedEnglish}\n`);

// 完整处理结果
console.log("完整处理结果:");
console.log(`${cleanedEnglish}`);
console.log(`${chinesePart}`);

// 详细处理步骤
console.log("\n详细处理步骤:");
console.log(`1. 原始英语部分: "${englishPart}"`);

// 应用各种正则表达式
let step = englishPart;
console.log(`2. 处理 "word$word$$$$$" 格式:`);
step = step.replace(/(\b\w+)\$\w+\${4,}/g, (match, p1) => {
  console.log(`   "${match}" -> "${p1}"`);
  return p1;
});
console.log(`   结果: "${step}"`);

console.log(`3. 处理 "词组$词组$$$$$" 格式:`);
step = step.replace(/(\b[\w\s]+)\$[\w\s]+\${4,}/g, (match, p1) => {
  console.log(`   "${match}" -> "${p1}"`);
  return p1;
});
console.log(`   结果: "${step}"`);

console.log(`4. 处理特殊符号:`);
step = step.replace(/\^/g, " ").replace(/\$/g, " ").replace(/◆/g, "");
console.log(`   结果: "${step}"`);

console.log(`5. 分割并过滤单词:`);
const words = step.split(" ").filter(word => word.trim() !== "");
console.log(`   单词列表: [${words.map(w => `"${w}"`).join(", ")}]`);

console.log(`6. 去重处理:`);
const cleanedWords = [];
for (let i = 0; i < words.length; i++) {
  if (i < words.length - 1 && words[i].toLowerCase() === words[i + 1].toLowerCase()) {
    console.log(`   发现重复词: "${words[i]}" 和 "${words[i + 1]}"，保留一个`);
    cleanedWords.push(words[i]);
    i++;
  } else {
    cleanedWords.push(words[i]);
  }
}
console.log(`   去重后: [${cleanedWords.map(w => `"${w}"`).join(", ")}]`);

console.log(`7. 词组重复处理:`);
let result = cleanedWords.join(" ");
const beforeResult = result;
result = result.replace(/(\b\w+(?:\s+\w+)*?)\s+\1\b/gi, "$1");
if (beforeResult !== result) {
  console.log(`   处理词组重复: "${beforeResult}" -> "${result}"`);
} else {
  console.log(`   无词组重复需要处理`);
}
console.log(`   最终结果: "${result.trim()}"`);
